//
//  ChujiaDetailV.swift
//  ZYA
//
//  Created by 邱仙凯 on 2024/9/4.
//  Copyright © 2024 zwb. All rights reserved.
//

import UIKit
 
class ChujiaDetailV:UITableView,UITableViewDelegate,UITableViewDataSource {
    var dataArray = [ChujiaDetailResult]()
    var zDtradeType = ""
    override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame: frame, style: style)
        headerV.addSubview(titleLab)
        titleLab.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(8)
            make.centerY.equalToSuperview()
        }
        self.delegate = self
        self.dataSource = self
        self.rowHeight = 55
        self.showsVerticalScrollIndicator = false
        self.register(ChujiaDetailHeaderView.self, forHeaderFooterViewReuseIdentifier: ChujiaDetailHeaderView.identifier)
        self.register(ChujiaDetailVCell.self, forCellReuseIdentifier: ChujiaDetailVCell.identifier)
        self.separatorStyle = .none
        self.tableHeaderView = headerV
    }
    lazy var titleLab:UILabel = {
        var v = UILabel()
        v.text = "出价记录"
        v.font = UIFont.boldSystemFont(ofSize: 17)
        return v
    }()
    
    var headerV : UIView = {
        let v = UIView()
        v.frame = CGRect(x: 0, y: 0, width: KSGlobal.screenW, height: 30)
        let title = UILabel()
        
        return v
    }()
    
     
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 35
        
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: ChujiaDetailHeaderView.identifier) as! ChujiaDetailHeaderView
        if zDtradeType == "3" {
            headerView.title3.text = "出价/运费"
        } else {
            headerView.title3.text = ""

        }
        
        return headerView
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: ChujiaDetailVCell.identifier, for: indexPath) as? ChujiaDetailVCell else {
            fatalError()
        }
        let one = dataArray[indexPath.row]
        cell.lab1.text = "\(indexPath.row + 1)"
        cell.lab2.text = one.z_name.unwrap
        cell.labtime.text = one.z_time_stamp
        cell.lab3.text = "\(one.z_price.unwrap)元/\(one.z_unit_nm.unwrap)"
        if one.z_bid_state == "1" {
         
            cell.lineV.isHidden = false
            cell.lineV.backgroundColor = .red
            cell.labChengjiao.isHidden = false
            cell.lab3.textColor = .red

     
        } else {
            cell.lineV.backgroundColor = .darkGray
            cell.lab3.textColor = .darkGray
            cell.labChengjiao.isHidden = true

            if one.z_star_state == "*" {
                cell.lineV.isHidden = false
            } else {
                cell.lineV.isHidden = true

            }
        }
        
         return cell
   }
 
   
}


class ChujiaDetailHeaderView: UITableViewHeaderFooterView {
   
 
   var title: UILabel!
   var title2: UILabel!
   var title3: UILabel!
   override init(reuseIdentifier: String?) {
       super.init(reuseIdentifier: reuseIdentifier)
       
      
       title = UILabel()
       title.text = "序号"
       title.font = UIFont.boldSystemFont(ofSize: 15)
       title.tintColor = .darkGray
       title.numberOfLines = 2
       contentView.addSubview(title)
       title.snp.makeConstraints { (make) in
           make.left.equalTo(self.contentView).offset(10)
           make.centerY.equalTo(self.contentView)
       }
       title2 = UILabel()
       title2.text = "出价信息"
       title2.font = UIFont.boldSystemFont(ofSize: 15)
       title2.tintColor = .darkGray
       title2.numberOfLines = 2
       contentView.addSubview(title2)
       title2.snp.makeConstraints { (make) in
           make.left.equalTo(self.contentView).offset(45)
           make.centerY.equalTo(self.contentView)
       }
       title3 = UILabel()
//       title3.text = "时间"
       title3.font = UIFont.boldSystemFont(ofSize: 15)
       title3.tintColor = .darkGray
       title3.numberOfLines = 2
       contentView.addSubview(title3)
       title3.snp.makeConstraints { (make) in
           make.right.equalTo(self.contentView).offset(-8)
           make.centerY.equalTo(self.contentView)
       }
   }
   
   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
   
  
}
class ChujiaDetailVCell: UITableViewCell {
   
   var btnClosure:((String)->())?
   var lab1:UILabel!
   var lab2:UILabel!
   var lab3:UILabel!
    var labtime:UILabel!
    var lineV:UIView!
    var labChengjiao:UILabel!

   override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
       super.init(style: style, reuseIdentifier: reuseIdentifier)
       
       
       self.backgroundColor = .white
       lab1 = UILabel()
       lab1.textColor = .darkGray
       lab1.font = UIFont.systemFont(ofSize: 14)
       lab2 = UILabel()
       lab2.textColor = .darkGray
       lab2.font = UIFont.systemFont(ofSize: 14)
       lab2.numberOfLines = 2
       lab3 = UILabel()
       lab3.textColor = .darkGray
       lab3.font = UIFont.systemFont(ofSize: 14)
       labtime = UILabel()
       labtime.textColor = .lightGray
       labtime.font = UIFont.systemFont(ofSize: 14)
       labChengjiao = UILabel()
       labChengjiao.textColor = .red
       labChengjiao.textAlignment = .center
       labChengjiao.text = "(成交)"
       labChengjiao.font = UIFont.systemFont(ofSize: 14)
       lineV = UIView()
       lineV.backgroundColor = .darkGray
       self.addSubview(lab1)
       self.addSubview(lab2)
       self.addSubview(lab3)
       self.addSubview(labtime)
       self.addSubview(lineV)
       self.addSubview(labChengjiao)

       
       lab1.snp.makeConstraints { (make) in
           make.centerY.equalTo(self)
           make.left.equalTo(self).offset(10)
       }
       lab2.snp.makeConstraints { (make) in
           make.left.equalTo(self).offset(45)
           make.centerY.equalTo(self)

       }
       labtime.snp.makeConstraints { (make) in
           make.top.equalTo(lab2.snp.bottom).offset(2)
           make.left.equalTo(lab2)

       }
       lab3.snp.makeConstraints { (make) in
           make.centerY.equalTo(self)

           make.right.equalTo(self).offset(-8)
       }
       lineV.snp.makeConstraints { (make) in
           make.left.right.equalTo(lab3)

           make.top.equalTo(lab3.snp.bottom).offset(1)
           make.height.equalTo(1)
       }
       labChengjiao.snp.makeConstraints { (make) in
           make.left.right.equalTo(lab3)

           make.top.equalTo(lineV.snp.bottom).offset(1)
       
       }
   }
   
 
   required init?(coder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
   
   
}
import ObjectMapper
struct ChujiaDetailResult: Mappable {
   
   var z_price: String?
   var z_time_stamp: String?
   var z_unit_nm:String?
    var z_name:String?
    var z_star_state:String?
    var z_bid_state:String?
   init?(map: Map) {
       
   }
   // Mappable
   mutating func mapping(map: Map) {
       
       z_price <- (map["z_price"],StringTransform_test())
       z_time_stamp <- map["z_time_stamp"]
       z_unit_nm <- map["z_unit_nm"]
       z_name <- map["z_name"]
       z_star_state <- map["z_star_state"]
       z_bid_state <- map["z_bid_state"]

   }
   
}



